package com.kx.ssm.controller;

import com.kx.ssm.constants.ResultCode;
import com.kx.ssm.dto.R;
import com.kx.ssm.exception.MyBaseException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 自定义异常增强controller
 * @ClassName MyExceptionHandler
 * @Author Sun
 * @Date 2021/5/20 23:01
 */

@ControllerAdvice
@ResponseBody
public class MyExceptionHandler {

    /**
     * 捕获我们通用的异常
     * @param e
     * @return
     */
    @ExceptionHandler(MyBaseException.class)
    public R processMyBaseException(MyBaseException e){
        e.printStackTrace();
        return  R.setError(e.getCode(), e.getMessage());
    }

    /**
     * 捕获未知异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public R processException(Exception e){
        e.printStackTrace();
        return R.setError(ResultCode.FAIL, "网络异常,请重试");
    }
}
